{% extends 'base.html' %}
{% from 'bootstrap/pagination.html' import render_pagination %}

{% block title %}Manage Comments{% endblock %}

{% block content %}
<div class="page-header">
    <h1>Comments
        <small class="text-muted">{{ pagination.total }}</small>
    </h1>

    <ul class="nav nav-pills">
        <li class="nav-item">
            <a class="nav-link disabled" href="#">Filter </a>
        </li>
        <li class="nav-item">
            <a class="nav-link {% if request.args.get('filter', 'all') == 'all' %}active{% endif %}"
               href="{{ url_for('admin.manage_comment', filter='all') }}">All</a>
        </li>
        <li class="nav-item">
            <a class="nav-link {% if request.args.get('filter') == 'unread' %}active{% endif %}"
               href="{{ url_for('admin.manage_comment', filter='unread') }}">Unread {% if unread_comments %}<span
                    class="badge badge-success">{{ unread_comments }}</span>{% endif %}</a>
        </li>
        <li class="nav-item">
            <a class="nav-link {% if request.args.get('filter') == 'admin' %}active{% endif %}"
               href="{{ url_for('admin.manage_comment', filter='admin') }}">From Admin</a>
        </li>
    </ul>
</div>

{% if comments %}
<table class="table table-striped">
    <thead>
    <tr>
        <th>No.</th>
        <th>Author</th>
        <th>Body</th>
        <th>Date</th>
        <th>Actions</th>
    </tr>
    </thead>
    {% for comment in comments %}
    <tr {% if not comment.reviewed %}class="table-warning" {% endif %}>
        <td>{{ loop.index + ((pagination.page - 1) * config['BLUELOG_COMMENT_PER_PAGE']) }}</td>
        <td>
            {% if comment.from_admin %}{{ admin.name }}{% else %}{{ comment.author }}{% endif %}<br>
            {% if comment.site %}
            <a href="{{ comment.site }}" target="_blank">
                {{ comment.site }}
            </a><br>
            {% endif %}
            <a href="mailto:{{ comment.email }}">{{ comment.email }}</a>

            {% if comment.from_admin %}
            <span class="badge badge-primary">Author</span>
            {% endif %}
        </td>
        <td>{{ comment.body }}</td>
        <td>{{ moment(comment.timestamp).format('LL') }}</td>
        <td>
            {% if not comment.reviewed %}
            <form class="inline" method="post"
                  action="{{ url_for('.approve_comment', comment_id=comment.id, next=request.full_path) }}">
                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
                <button type="submit" class="btn btn-success btn-sm">Approve</button>
            </form>
            {% endif %}
            <a class="btn btn-info btn-sm" href="{{ url_for('blog.show_post', slug=comment.post.slug) }}">Post</a>
            <form class="inline" method="post"
                  action="{{ url_for('.delete_comment', comment_id=comment.id, next=request.full_path) }}">
                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
                <button type="submit" class="btn btn-danger btn-sm"
                        onclick="return confirm('Are you sure?');">Delete
                </button>
            </form>
        </td>
    </tr>
    {% endfor %}
</table>
<div class="page-footer">{{ render_pagination(pagination) }}</div>
{% else %}
<div class="tip"><h5>No comments.</h5></div>
{% endif %}
{% endblock %}
